﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using GameFramework.data;
using UnityEngine;

namespace GameFramework.common
{
    public class JsonUtil
    {
        public static Dictionary<Type,string> dataPathCache = new  Dictionary<Type,String>();
        public static T loadData<T>() where T:new()
        {
            Type type = typeof(T);
            string path = getPathByType(type);
            if (!File.Exists(path))
            {
                return (T)type.Assembly.CreateInstance(type.FullName);
            }

            string text = File.ReadAllText(path);
            object fromJson = null;
            try
            {
                fromJson = JsonUtility.FromJson(text, type);
            }
            catch (Exception e)
            {
                UnityEngine.Debug.LogError(e);
                return (T)type.Assembly.CreateInstance(type.FullName);
            }
            if (fromJson == null)
            {
                return (T)type.Assembly.CreateInstance(type.FullName);
            }

            return (T)fromJson;
        }
        
        public static void writeData<T>(T data) {
            Type type = typeof(T);
            string path = getPathByType(type);
            // File.WriteAllText(path, JsonUtility.ToJson(data, true));
            if (!File.Exists(path))
            {
                createParentFolder(path);
            }
            File.WriteAllText(path, JsonUtility.ToJson(data, true));
        }

        public static string getPathByType(Type type)
        {
            string path = "";
            if (dataPathCache.ContainsKey(type))
            {
                path = dataPathCache[type];
                return path;
            }
            var customAttributes = type.GetCustomAttribute<DataLocationAttribute>();
            if (customAttributes == null)
            {
                Debug.LogError($"{type.Name} 没有设置DataLocationAttribute，无法加载数据！");
                return path;
            }

            path = customAttributes.path;
            if (path == null || path.Equals(""))
            {
                Debug.LogError($"{type.Name} DataLocationAttribute的path未正确设置，无法加载数据！");
                return path;
            }
            path = path.StartsWith("/")?path:"/"+path;
            //TODO 注意几个路径的区别
            //Application.dataPath
            //Application.persistentDataPath
            path = Application.dataPath + "/" + path;
            dataPathCache.Add(type, path);
            return path;
        }
        
        public static bool createParentFolder(string filePath)
        {
            try
            {
                FileInfo fileInfo = new FileInfo(filePath);
                string parentDirPath = fileInfo.DirectoryName;
                if (Directory.Exists(parentDirPath) == false) // 如果父亲文件夹不存在则创建
                {
                    Directory.CreateDirectory(parentDirPath);
                }
                return true;
            }
            catch
            {
                return false;
            }
    
        }
    }
}